package com.mengma.ec.service.impl.portal;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mengma.ec.commons.Const;
import com.mengma.ec.commons.ServerResponse;
import com.mengma.ec.dao.OrderItemMapper;
import com.mengma.ec.dao.OrderMapper;
import com.mengma.ec.dao.ShippingMapper;
import com.mengma.ec.entity.Order;
import com.mengma.ec.entity.OrderItem;
import com.mengma.ec.entity.Shipping;
import com.mengma.ec.service.portal.IManagerOrderService;
import com.mengma.ec.vo.OrderItemVo;
import com.mengma.ec.vo.OrderVo;
import com.mengma.ec.vo.ShippingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: xixi
 * @Date: 2018/10/8 10:12
 * @Description:
 */
@Service("iManagerOrderService")
public class ManagerOrderServiceImpl implements IManagerOrderService{

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ShippingMapper shippingMapper;
    @Override
    public ServerResponse<PageInfo> list(Integer id, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        //获取原始数据源
        List<OrderVo> orderVoList=new ArrayList<>();
        List<Order> orderList = orderMapper.selectAllOrder();
        if (orderList.size()==0){
            return ServerResponse.createByErrorMessage("没有数据");
        }
        for (Order order : orderList) {
            List<OrderItem> orderItems=orderItemMapper.selectByOrderNo(order.getOrderNo());
            OrderVo orderVo = assembleOrderVo(order, orderItems);
            orderVoList.add(orderVo);
        }
        PageInfo pageInfo = new PageInfo(orderList);
        pageInfo.setList(orderVoList);

        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse search(Long orderNo) {
        OrderItem order = orderItemMapper.findByOrderNo(orderNo);
        if(order == null){
            return ServerResponse.createByErrorMessage("查询失败");
        }
        return ServerResponse.createBySuccess("查询成功！",order);
    }

    @Override
    public ServerResponse send_goods(Long orderNo) {
        OrderItem order = orderItemMapper.findByOrderNo(orderNo);
        if(order != null){
            if(order.getQuantity()==40) {
                return ServerResponse.createBySuccess("发货成功");
            }
    }
        return ServerResponse.createByErrorMessage("订单不存在");
    }

    private OrderVo assembleOrderVo(Order order, List<OrderItem> orderItemList) {
        OrderVo orderVo = new OrderVo();

        orderVo.setOrderNo(order.getOrderNo());
        orderVo.setPayment(order.getPayment());
        orderVo.setPaymentType(order.getPaymentType());
//        orderVo.setPaymentTypeDesc(Const.PaymentTypeEnum.codeOf(order.getPaymentType()).getValue());

        orderVo.setPostage(order.getPostage());
        orderVo.setStatus(order.getStatus());
//        orderVo.setStatusDesc(Const.OrderStatusEnum.codeOf(order.getStatus()).getValue());

        orderVo.setShippingId(order.getShippingId());


        // 通过 收货地址ID 查询 收货地址详细信息
        Shipping shipping = shippingMapper.selectByPrimaryKey(order.getShippingId());
        if (shipping != null) {
            orderVo.setReceiverName(shipping.getReceiverName());
            orderVo.setShippingVo(this.assembleShippingVo(shipping));
        }

        orderVo.setPaymentTime("2018-09-27");
        orderVo.setSendTime("2018-09-27");
        orderVo.setEndTime("2018-09-27");
        orderVo.setCreateTime("2018-09-27");
        orderVo.setCloseTime("2018-09-27");

//        orderVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));

        List<OrderItemVo> orderItemVoList = new ArrayList<>();

        for (OrderItem orderItem : orderItemList) {
            OrderItemVo orderItemVo = assembleOrderItemVo(orderItem);
            orderItemVoList.add(orderItemVo);
        }
        orderVo.setOrderItemVoList(orderItemVoList);
        return orderVo;

    }

    /**
     * 通过 orderItem 数据源 ，转换为 VO 显示对象
     * @param orderItem
     * @return
     */
    private OrderItemVo assembleOrderItemVo(OrderItem orderItem) {
        OrderItemVo orderItemVo = new OrderItemVo();
        orderItemVo.setOrderNo(orderItem.getOrderNo());
        orderItemVo.setProductId(orderItem.getProductId());
        orderItemVo.setProductName(orderItem.getProductName());
        orderItemVo.setProductImage(orderItem.getProductImage());
        orderItemVo.setCurrentUnitPrice(orderItem.getCurrentUnitPrice());
        orderItemVo.setQuantity(orderItem.getQuantity());
        orderItemVo.setTotalPrice(orderItem.getTotalPrice());
        return orderItemVo;
    }

    /**
     * 通过说地址详细信息，包装成需要显示的 VO 对象
     * @param shipping
     * @return
     */
    private ShippingVo assembleShippingVo(Shipping shipping) {
        ShippingVo shippingVo = new ShippingVo();
        shippingVo.setReceiverName(shipping.getReceiverName());
        shippingVo.setReceiverAddress(shipping.getReceiverAddress());
        shippingVo.setReceiverProvince(shipping.getReceiverProvince());
        shippingVo.setReceiverCity(shipping.getReceiverCity());
        shippingVo.setReceiverDistrict(shipping.getReceiverDistrict());
        shippingVo.setReceiverMobile(shipping.getReceiverMobile());
        shippingVo.setReceiverZip(shipping.getReceiverZip());
        shippingVo.setReceiverPhone(shippingVo.getReceiverPhone());
        return shippingVo;

    }

}
